import { IUser } from "@/store/user";
import { customRequest } from "../utils/request";
import { IMessage } from "./message";

// 获取房间列表
export interface roomDto {
  name: string;
}
export interface IRoom {
  id: number;
  name: string;
  avatar: string;
  chatType: "Private" | "Group";
  createTime: string;
  userCount: number;
  userIds: number[];
}
export const getChatRooms = (data: roomDto) => {
  return customRequest<IRoom[]>({
    url: "/chatroom/list",
    method: "get",
    params: data,
  });
};

export type RoomDetail = IRoom & {
  users: IUser[];
};
// 获取房间信息（包括成员信息）
export const getRoomDetail = (roomId: number) => {
  return customRequest<RoomDetail>({
    url: `/chatroom/info/${roomId}`,
    method: "get",
  });
};

export interface IUnread {
  roomId: number;
  unreadCount: number;
  lastUnreadMessage: IMessage;
}

// 获取用户所有房间未读信息
export const getUnreadMessage = () => {
  return customRequest<IUnread[]>({
    url: "/chatroom/unread",
    method: "get",
  });
};

// 清除房间未读信息
export const clearUnread = (roomId: number) => {
  return customRequest({
    url: `/chatroom/clearUnread/${roomId}`,
    method: "get",
  });
};

// 创建单聊
export const createPrivateChat = (data: { friendId: number }) => {
  return customRequest<number>({
    url: `/chatroom/create-private`,
    method: "get",
    params: data,
  });
};

interface ICreateGroupDto {
  name: string;
  avatar?: string;
  friendIds: string;
}

// 创建群聊
export const createGroupChat = (data: ICreateGroupDto) => {
  return customRequest<number>({
    url: `/chatroom/create-group`,
    method: "get",
    params: data,
  });
};

// 查找房间
export interface IRoomSearchDto {
  memberIds: string;
}
export const searchRoomByIds = (data: IRoomSearchDto) => {
  return customRequest<number[]>({
    url: "/chatroom/find",
    method: "get",
    params: data,
  });
};

// 加入房间
export const joinRoom = (roomId: number, userIds: string) => {
  return customRequest({
    url: `/chatroom/join/${roomId}`,
    method: "get",
    params: {
      userIds,
    },
  });
};

// 踢出房间
export const quitRoom = (roomId: number, userIds: string) => {
  return customRequest({
    url: `/chatroom/quit/${roomId}`,
    method: "get",
    params: {
      userIds,
    },
  });
};

// 销毁房间
export const destroyRoom = (roomId: number) => {
  return customRequest({
    url: `/chatroom/destroy/${roomId}`,
    method: "get",
  });
}